package com.frogs.ucenter.component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.OpenPlatformEnum;
import com.frogs.system.util.TraceConstants;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.ucenter.util.CommonRequestApiUtil;

@Component("requestApiHelper")
public class RequestApiHelper extends CommonRequestApiUtil{
	
	private Logger log = LoggerFactory.getLogger(RequestApiHelper.class);
	private Map<Integer, Integer> platformMap = new HashMap<Integer, Integer>(); 
	
	public RequestApiHelper() {
		//渠道标示码键值对
		platformMap.put(-1, TraceConstants.TRACE_UC_CHANNEL_GW);
		platformMap.put(OpenPlatformEnum.WECHAT_MALL.getId(), TraceConstants.TRACE_UC_CHANNEL_WECHAT_SC);
		platformMap.put(OpenPlatformEnum.WECHAT_HDC.getId(), TraceConstants.TRACE_UC_CHANNEL_WECHAT_XYJ);
		platformMap.put(OpenPlatformEnum.WECHART_QR.getId(), TraceConstants.TRACE_UC_CHANNEL_WECHAT_SM);
		platformMap.put(OpenPlatformEnum.QQ.getId(), TraceConstants.TRACE_UC_CHANNEL_QQ);
		platformMap.put(OpenPlatformEnum.SINAWEIBO.getId(), TraceConstants.TRACE_UC_CHANNEL_SINA_WB);
		platformMap.put(OpenPlatformEnum.ALIPAY.getId(), TraceConstants.TRACE_UC_CHANNEL_ZFB);
		platformMap.put(OpenPlatformEnum.LANHAI.getId(), TraceConstants.TRACE_UC_CHANNEL_LH);
	}
	
	/**
	 * 获取优惠券信息
	 * @param orderSn
	 * @return
	 */
	public JSONObject getPromotionCouponList(int sendCardType, int pageNo, int pageSize) {
		ApiUrl apiUrl = this.buildPromotionApiBaseUrl("PROMOTION_COUPON_LIST");
		String[] otherParams = {
				ApiUrlParamEnum.sendCardType.getParamName(), String.valueOf(sendCardType),
				ApiUrlParamEnum.pageNo.getParamName(), String.valueOf(pageNo),
				ApiUrlParamEnum.pageSize.getParamName(), String.valueOf(pageSize),
				ApiUrlParamEnum.applicablePlatformId.getParamName(), ApplicablePlatformEnum.WEB.getId()+""
			};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			return (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[getPromotionCouponList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	/**
	 * 获取红包列表
	 * @param orderSn
	 * @return
	 */
	public JSONObject getRedPackList(int pageNo, int pageSize) {
		ApiUrl apiUrl = this.buildWxpayApiBaseUrl("RED_PACKET_LIST");
		String[] otherParams = {
				ApiUrlParamEnum.pageNo.getParamName(), String.valueOf(pageNo),
				ApiUrlParamEnum.pageSize.getParamName(), String.valueOf(pageSize)
			};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			return (JSONObject) this.requestApi(apiUrl.getUrl(), Method.GET, allParams);
		} catch (Exception e) {
			log.error("func[getRedPackList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	public JSONObject creatAreaJs() {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("AREA_CREATE_JS");
		String[] otherParams = {
			};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			return (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[getPromotionCouponList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}
	
	public JSONObject sendSms(String phone, String messageContent) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("SMS_SEND");
		String[] otherParams = {
				"phoneNumber",phone,
				"messageContent",messageContent.trim(),
				"sendLevel",8+"",
			};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		try{
			return (JSONObject) this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
		} catch (Exception e) {
			log.error("func[getPromotionCouponList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

}
